package com.knife.dom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.knife.entity.ZDM;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * create by yongli on 2021-11-18 19:06
 */

public class OrderClient {
    public static void main(String[] args) {

        String path = "/Users/yongli/Desktop/test.txt";

        RestTemplate restTemplate = new RestTemplate();


        ZDM zdm = new ZDM();
        zdm.setDOC("12121");
        zdm.setQUAN("121212");
        // 什么时候回带有斜杠？
        // 从文本中读取的字符有"就会被转义
        System.out.println(zdm.toString());
        String zdmS = JSON.toJSONString(zdm);
        System.out.println(zdmS);

        try {

            byte[] bytes = zdmS.getBytes();
            String zdmSS = new String(bytes);
            System.out.println(zdmSS);
            File file = new File(path);
            boolean status = file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.flush();
            fos.close();

            FileInputStream fis = new FileInputStream(file);
            byte[] bytes2 = new byte[1024];
            fis.read(bytes2);
            System.out.println(new String(bytes2));
        } catch (Exception e) {
            e.printStackTrace();
        }

        String zdmSS = JSON.toJSONString(zdmS);
        System.out.println(zdmSS);

        // Bean <--> String <--> JSONObject
        JSONObject json = JSON.parseObject(zdmS);
        System.out.println(json.toJSONString());
        System.out.println(json.getString("dOC"));

        // 从String来的JSON会有这个现象
        JSONObject json2 = (JSONObject) JSONObject.toJSON(zdm);
        System.out.println(json2.toJSONString());


        String url = "http://localhost:8081/order/iam/zdm";
        // string not Application/Json
        HttpEntity<Object> httpEntity = new HttpEntity<>(zdmSS);
        ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
        System.out.println(response.getBody());
    }
}
